---
  title: "Collective decision making in disinformation infused climates. Two Romanian cases compared"
  author: "Raluca-Nicoleta Radu"
  date: "6/5/2022"
  R version 4.1.3 (2022-03-10) -- "One Push-Up"
  tidyverse 1.3.1

---

  library("tidyverse")

# importing CSV files of Crowdtangle results for
# 1. Search: ordonantei, ordonanta, ordonantelor, ordonantele, ordonante
#    Date range: 2017-01-01 23:59:59 EET - 2017-02-28 14:08:00 EET
# 2. Search: COVID; language: Romanian
#    Date range: 2020-03-01 23:59:59 EET - 2020-04-30 13:39:00 EEST
# 3. Search: COVID; language: Romanian
#    Date range: 2021-08-01 23:59:59 EEST - 2021-09-30 14:00:00 EEST
  
  
  ordonanta_2017 <- read_csv('ordonanta_2017.csv')
  covid_2020 <- read_csv('covid_2020.csv')
  covid_2021 <- read_csv('covid_2021.csv')
  
# identifying top total interactions by Facebook ID for each sample of posts
  
  by_Facebook_Id_proteste <- group_by(ordonanta_2017, Facebook_Id)
  top_total_interactions_proteste <- summarise(by_Facebook_Id_proteste, Facebook_Id_TI = mean(Total_Interactions, na.rm = TRUE))
  arrange(top_total_interactions_proteste, desc(Facebook_Id_TI))
  
  by_Facebook_Id_covid <- group_by(covid_2020, Facebook_Id)
  top_total_interactions_covid <- summarise(by_Facebook_Id_covid, Facebook_Id_TI = mean(Total_Interactions, na.rm = TRUE))
  arrange(top_total_interactions_covid, desc(Facebook_Id_TI))
  
  by_Facebook_Id_covid_2021 <- group_by(covid_2021, Facebook_Id)
  top_total_interactions_covid_2021 <- summarise(by_Facebook_Id_covid_2021, Facebook_Id_TI = mean(Total_Interactions, na.rm = TRUE))
  arrange(top_total_interactions_covid_2021, desc(Facebook_Id_TI))
  
# making a table with facebook ID and total number of observation per id, this is, total number of posts
  
  top_number_posts_proteste <- count(ordonanta_2017, Facebook_Id)
  arrange (top_number_posts_proteste, desc(n)) 
  
  top_number_posts_covid <- count(covid_2020, Facebook_Id)
  arrange (top_number_posts_covid, desc(n)) 
  
  top_number_posts_covid_2021 <- count(covid_2021, Facebook_Id)
  arrange (top_number_posts_covid_2021, desc(n)) 
  
# merge two data frames by facebook ID
  
  proteste_2017_posts_interactions <- merge(top_total_interactions_proteste, top_number_posts_proteste, by="Facebook_Id")
  covid_2020_posts_interactions <- merge(top_total_interactions_covid, top_number_posts_covid, by="Facebook_Id")
  covid_2021_posts_interactions <- merge(top_total_interactions_covid_2021, top_number_posts_covid_2021, by="Facebook_Id")

  ordonanta_2017_NEW <- left_join(ordonanta_2017, proteste_2017_posts_interactions, by="Facebook_Id")  
  covid_2020_NEW <- left_join(covid_2020, covid_2020_posts_interactions, by="Facebook_Id")
  covid_2021_NEW <- left_join(covid_2021, covid_2021_posts_interactions, by="Facebook_Id")

  
# identifying media, politicians ad gov. structures in a plot; x = number of posts per Facebook ID, y = mean of total interactions, per Facebook ID
  
  ggplot(covid_2020_NEW, aes(x=n, y=Facebook_Id_TI, color=Page_Category)) +
    geom_point(size = 2) +
    scale_color_manual(values = c("NEWS_SITE" = "red", "MEDIA_NEWS_COMPANY"="red",
                                  "MEDIA"="red", "TOPIC_NEWSPAPER" = "red", "TV_CHANNEL"="red", 
                                  "JOURNALIST"="red", "RADIO_STATION"="red", "TOPIC_PUBLISHER"="red", 
                                  "POLITICAL_PARTY"= "blue", "POLITICIAN"= "blue", "GOVERNMENT_ORGANIZATION"="yellow","GOV_SITE"="yellow")) +
    ggtitle("COVID Posts 2020")
  
  ggplot(ordonanta_2017_NEW, aes(x=n, y=Facebook_Id_TI, color=Page_Category)) +
    geom_point(size = 2) +
    scale_color_manual(values = c("NEWS_SITE" = "red", "MEDIA_NEWS_COMPANY"="red",
                                  "MEDIA"="red", "TOPIC_NEWSPAPER" = "red", "TV_CHANNEL"="red", 
                                  "JOURNALIST"="red", "RADIO_STATION"="red", "TOPIC_PUBLISHER"="red", 
                                  "POLITICAL_PARTY"= "blue", "POLITICIAN"= "blue", "GOVERNMENT_ORGANIZATION"="yellow", "GOV_SITE"="yellow")) +
    ggtitle("Protests Posts 2017")
  
  ggplot(covid_2021_NEW, aes(x=n, y=Facebook_Id_TI, color=Page_Category)) +
    geom_point(size = 2,  position = "jitter") +
    scale_color_manual(values = c("NEWS_SITE" = "red", "MEDIA_NEWS_COMPANY"="red",
                                  "MEDIA"="red", "TOPIC_NEWSPAPER" = "red", "TV_CHANNEL"="red", 
                                  "JOURNALIST"="red", "RADIO_STATION"="red", "TOPIC_PUBLISHER"="red", 
                                  "POLITICAL_PARTY"= "blue", "POLITICIAN"= "blue", "GOVERNMENT_ORGANIZATION"="yellow", "GOV_SITE"="yellow")) +
    ggtitle("COVID Posts 2021")

# additional, exporting an excel file with interaction and number of posts data
  library("writexl")
  write_xlsx(proteste_2017_posts_interactions,"protests_2017_by_FB_Id.xlsx")
  write_xlsx(covid_2020_posts_interactions,"covid_2020_by_FB_Id.xlsx")
  write_xlsx(covid_2021_posts_interactions,"covid_2021_by_FB_Id.xlsx")
  
  
  write_xlsx(ordonanta_2017_NEW,"protests_2017_43var.xlsx")
  write_xlsx(covid_2020_NEW,"covid_2020_43var.xlsx")
  write_xlsx(covid_2021_NEW,"covid_2021_43var.xlsx")